﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lottery.MediaData.UI.Control {
    /// <summary>
    /// 数据大中小 输入控件
    /// </summary>
    public partial class DZXControl : UserControl {
        public DZXControl() {
            InitializeComponent();

            editPreNumber.Items.Clear();
            editPreNumber.Items.Add("");
            editPreNumber.Items.Add("大");
            editPreNumber.Items.Add("中");
            editPreNumber.Items.Add("小");
            editPreNumber.SelectedIndex = 0;

            labelInfo.Text = "";
        }

        private int index;

        /// <summary>
        /// 场次
        /// </summary>
        public int Index {
            get { return index; }
            set {
                index = value;
                labelIndex.Text = string.Format("第 {0} 场", index);
            }
        }

        /// <summary>
        /// 3
        /// </summary>
        public int Three {
            get { return edit3.Value; }
            set { edit3.Text = value.ToString(); }
        }

        /// <summary>
        /// 1
        /// </summary>
        public int One {
            get { return edit1.Value; }
            set { edit1.Text = value.ToString(); }
        }

        /// <summary>
        /// 0
        /// </summary>
        public int Zero {
            get { return edit0.Value; }
            set { edit0.Text = value.ToString(); }
        }

        /// <summary>
        /// 预测号 310
        /// </summary>
        public string PreNumber {
            get {
                if (editPreNumber.Text == "大") {
                    return D;
                } else if (editPreNumber.Text == "中") {
                    return Z;
                } else if (editPreNumber.Text == "小") {
                    return X;
                }
                return string.Empty;
            }

            set { }
        }

        /// <summary>
        /// 预测文字
        /// </summary>
        public string PreText { get { return editPreNumber.Text; } set { editPreNumber.SelectedIndex = editPreNumber.Items.IndexOf(value); } }

        class TOZ {
            public string Name { get; set; }
            public int Value { get; set; }
        }

        /// <summary>
        /// 大
        /// </summary>
        public string D { get; set; }

        /// <summary>
        /// 中
        /// </summary>
        public string Z { get; set; }

        /// <summary>
        /// 小
        /// </summary>
        public string X { get; set; }

        public bool IsOK {
            get {
                return edit0.Text != string.Empty && edit1.Text != string.Empty
                    && edit3.Text != string.Empty && editPreNumber.Text != string.Empty;
            }
        }

        private System.Windows.Forms.Control jumpToNext;
        /// <summary>
        /// 按回车键的时候，焦点跳转到的控件
        /// </summary>
        public System.Windows.Forms.Control JumpToNext {
            get { return jumpToNext; }
            set {
                jumpToNext = value;
                editPreNumber.JumpToNext = value;
            }
        }


        /// <summary>
        /// 设置数据大中小信息
        /// </summary>
        public void SetDZXLabelInfo() {
            D = string.Empty;
            Z = string.Empty;
            X = string.Empty;

            if (Three == One && One == Zero) {
                labelInfo.Text = "大 = 310";
                D = "310";
                return;
            }

            if (Three == One && Three != Zero) {
                if (Three > Zero) {
                    labelInfo.Text = "大=31, 中=0";
                    D = "31";
                    Z = "0";
                } else {
                    labelInfo.Text = "大=0, 中=31";
                    D = "0";
                    Z = "31";
                }
                return;
            }

            if (Three == Zero && Three != One) {
                if (Three > One) {
                    labelInfo.Text = "大=30, 中=1";
                    D = "30";
                    Z = "1";
                } else {
                    labelInfo.Text = "大=1, 中=30";
                    D = "1";
                    Z = "30";
                }
                return;
            }

            if (One == Zero && One != Three) {
                if (One > Three) {
                    labelInfo.Text = "大=10, 中=3";
                    D = "10";
                    Z = "3";
                } else {
                    labelInfo.Text = "大=3, 中=10";
                    D = "3";
                    Z = "10";

                }
                return;
            }

            //剩下的情况就是三个数均不相等
            var a = new TOZ { Name = "3", Value = Three };
            var b = new TOZ { Name = "1", Value = One };
            var c = new TOZ { Name = "0", Value = Zero };
            var data = new List<TOZ>();
            data.Add(a);
            data.Add(b);
            data.Add(c);
            var result = from item in data
                         orderby item.Value descending
                         select item;
            TOZ[] r = result.ToArray();
            labelInfo.Text = string.Format("大={0}, 中={1}, 小={2}", r[0].Name, r[1].Name, r[2].Name);
            D = r[0].Name;
            Z = r[1].Name;
            X = r[2].Name;
        }

        public void Clear() {
            edit3.Text = string.Empty;
            edit1.Text = string.Empty;
            edit0.Text = string.Empty;

            editPreNumber.SelectedIndex = 0;
        }

        private void DZXControl_Leave(object sender, EventArgs e) {
            try {
                SetDZXLabelInfo();
            } catch { }
        }

        private void DZXControl_EnabledChanged(object sender, EventArgs e) {
            label2.Visible = label3.Visible = label4.Visible = this.Enabled;
            edit3.Visible = edit1.Visible = edit0.Visible = this.Enabled;
            label1.Visible = labelInfo.Visible = this.Enabled;
            editPreNumber.Visible = this.Enabled;
        }

        public void FocusOnEmptyEdit() {
            if (edit3.Text == string.Empty)
                edit3.Focus();
            else if (edit1.Text == string.Empty)
                edit1.Focus();
            else if (edit0.Text == string.Empty)
                edit0.Focus();
            else if (editPreNumber.Text == string.Empty)
                editPreNumber.Focus();
            else
                edit3.Focus();
        }
    }
}
